package com.mgvlife.ddlw.action;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.UUID;

import javax.annotation.Resource;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.mgvlife.ddlw.entity.Admin;
import com.mgvlife.ddlw.entity.Clazz;
import com.mgvlife.ddlw.entity.Schedule;
import com.mgvlife.ddlw.entity.SignInInfo;
import com.mgvlife.ddlw.entity.Student;
import com.mgvlife.ddlw.entity.SuperAdmin;
import com.mgvlife.ddlw.service.SuperAdminService;
import com.mgvlife.ddlw.vo.Result;
import com.opensymphony.xwork2.ActionSupport;

@Controller("initDataAction")
@Scope("prototype")
public class InitDataAction extends ActionSupport {
	private static final long serialVersionUID = -558561300444881022L;

	@Resource
	private SuperAdminService superAdminService = null;

	public String initData() {
		Result result = null;
		try {
			long count = superAdminService
					.getDataCount(Admin.class, null, null);
			if (count == 0
					&& initSuperAdmin()
					&& initClazz("C:\\Users\\mrsimple\\Desktop\\clazzData.xls",
							"clazz")
					&& initStudent(
							"C:\\Users\\mrsimple\\Desktop\\studentData.xls",
							"student")) {
				result = Result.success();
			} else {
				result = new Result("error", "已经初始化过了");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = Result.unknownError();
		}
		return result.getResult();
	}

	// 初始化超级管理员
	private boolean initSuperAdmin() {
		SuperAdmin superAdmin = new SuperAdmin();
		superAdmin.setId(UUID.randomUUID().toString());
		superAdmin.setName("mgvlife");
		superAdmin.setPassword("mgvlife");
		superAdminService.save(superAdmin);
		return true;
	}

	// 初始化学生
	private boolean initStudent(String path, String sheetName) {
		try {
			InputStream is = new FileInputStream(path);
			Workbook rwb = Workbook.getWorkbook(is);
			// get sheet
			Sheet sheet = rwb.getSheet(sheetName);
			// get rows
			int cr = sheet.getRows();
			for (int i = 1; i < cr; i++) {
				Cell[] cells = sheet.getRow(i);
				String whereSQL = "grade=? and major=? and clazznum=?";
				Object[] whereParams = { cells[2].getContents().trim(),
						cells[3].getContents().trim(),
						cells[4].getContents().trim() };
				Clazz clazz = superAdminService.find(Clazz.class, whereSQL,
						whereParams);
				if (clazz != null) {
					Student student = new Student();
					student.setId(UUID.randomUUID().toString());
					student.setOpenId(null);
					student.setName(cells[1].getContents().trim());
					student.setStuId(cells[0].getContents().trim());
					student.setImgUrl("");
					student.setClazz(clazz);
					student.setSignInInfos(new ArrayList<SignInInfo>());
					clazz.getStudents().add(student);
					superAdminService.save(student);
				} else {
					continue;
				}
			}
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		} catch (BiffException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	// 初始化班
	private boolean initClazz(String path, String sheetName) {
		try {
			InputStream is = new FileInputStream(path);
			Workbook rwb = Workbook.getWorkbook(is);
			// get sheet
			Sheet sheet = rwb.getSheet(sheetName);
			// get rows
			int cr = sheet.getRows();
			for (int i = 1; i < cr; i++) {
				Cell[] cells = sheet.getRow(i);
				Clazz clazz = new Clazz();
				clazz.setId(UUID.randomUUID().toString());
				clazz.setGrade(cells[0].getContents().trim());
				clazz.setMajor(cells[1].getContents().trim());
				clazz.setClazzNum(cells[2].getContents().trim());
				clazz.setSchedules(new ArrayList<Schedule>());
				clazz.setStudents(new ArrayList<Student>());
				superAdminService.save(clazz);
			}
			return true;
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return false;
		} catch (BiffException e) {
			e.printStackTrace();
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
}
